package com.atguigu.spring5.bean1.testDemo;

import com.atguigu.spring5.bean1.Book;
import com.atguigu.spring5.bean1.Order;
import com.atguigu.spring5.bean1.Person;
import com.atguigu.spring5.bean1.User;
import com.atguigu.spring5.bean1.bean.Emp;
import com.atguigu.spring5.bean1.collectiontype.Course;
import com.atguigu.spring5.bean1.collectiontype.Student;
import com.atguigu.spring5.bean1.liftcycle.Orders;
import com.atguigu.spring5.bean1.service.UserService;
import org.junit.Test;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * 用Junit执行一个简单的bean
 * @author liangchao
 * @create 2021-06-02 20:30
 */
public class TestSpring5 {



    @Test
    public void testLifeCycle(){
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("bean7.xml");
//        ApplicationContext context = new ClassPathXmlApplicationContext("bean7.xml");
        Orders orders= context.getBean("orders", Orders.class);
        System.out.println("第四步 获取创建bean实例对象");
        System.out.println(orders);
        // 手动让bean实例销毁
        // ApplicationContext 没有close方法，实现类中有该方法
        // ((ClassPathXmlApplicationContext) context).close();
        context.close();

    }

    // 测试spring默认生成bean是否为单例
    @Test
    public void testSingleton(){
        ApplicationContext context = new ClassPathXmlApplicationContext("bean5.xml");
        Student s1= context.getBean("student", Student.class);
        Student s2 = context.getBean("student", Student.class);
        System.out.println(s1);
        System.out.println(s2);
    }

    // 测试工厂bean
    @Test
    public void testFactoryBean(){
        ApplicationContext context = new ClassPathXmlApplicationContext("bean6.xml");
        Course course = context.getBean("myBean", Course.class);
        course.toString();
    }

    // 注入属性--集合属性
    @Test
    public void testCollection(){
        ApplicationContext context = new ClassPathXmlApplicationContext("bean5.xml");
        Student s = context.getBean("student", Student.class);
        s.output();
    }

    // 测试级联赋值
    @Test
    public void testCascadeBean(){
        ApplicationContext context = new ClassPathXmlApplicationContext("bean4.xml");
        Emp emp = context.getBean("emp", Emp.class);
        emp.output();
    }

    // 测试内部bean
    @Test
    public void testInnerBean(){
        ApplicationContext context = new ClassPathXmlApplicationContext("bean3.xml");
        Emp emp = context.getBean("emp", Emp.class);
        emp.output();
    }



    @Test
    public void testUserService(){
        ApplicationContext context = new ClassPathXmlApplicationContext("bean2.xml");
        UserService userService = context.getBean("userService", UserService.class);
        userService.add();
    }


    @Test
    public void testUser(){
        // 1加载spring 配置文件
        // classPath 类路径，也就是直接从src里找文件
        // 如果在其它路径下，可用 new FileSystemXmlApplicationContext()
        ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml");
//        BeanFactory context = new ClassPathXmlApplicationContext("bean1.xml"); // 也可以直接换成BeanFactory

        // 2获取配置创建的对象
        // 第一个参数 xml中配置的bean别名 id
        // 第二个参数表示返回结果要转换成的类
        User user = context.getBean("user", User.class);

        System.out.println(user);
        user.add();
    }

    @Test
    public void testBook(){
        ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml");
        Book book = context.getBean("book", Book.class);

        System.out.println(book);
        book.read();
    }

    @Test
    public void testPerson(){
        ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml");
        Person person = context.getBean("person", Person.class);
        person.output();
    }

}
